{extend name="$base" /}
{block name="resources"}{/block}
{block name="main"}
<div class="set-style">
	<dl>
		<dt>模块名：</dt>
		<dd>
			<p><input id="module_name" type="text" class="input-common"></p>
			<p class="error">请输入模块名</p>
		</dd>
	</dl>
	<dl>
		<dt>上级菜单：</dt>
		<dd>
			<p>
				<select class="form-control input-sm input-common" id="pid">
					<option value="0">顶级菜单</option>
					{foreach name="list" item="v1"}
					<option value="{$v1['data']['module_id']}">&nbsp;&nbsp;&nbsp;{$v1['data']['module_name']}</option>
					{foreach name="v1['sub_menu']" item="v2"}
					<option value="{$v2['module_id']}">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$v2['module_name']}</option>
					{/foreach}
					{/foreach}
				</select>
			</p>
		</dd>
	</dl>
	<dl>
		<dt>控制器名：</dt>
		<dd>
			<p><input id="controller" type="text" class="input-common"></p>
			<p class="error">请输入控制器名</p>
		</dd>
	</dl>
	<dl>
		<dt>方法名：</dt>
		<dd>
			<p><input id="method" type="text" class="input-common"></p>
			<p class="error">请输入方法名</p>
		</dd>
	</dl>
	<dl>
		<dt>URL：</dt>
		<dd>
			<p><input id="url" type="text" class="input-common"></p>
			<p class="error">请输入url地址</p>
		</dd>
	</dl>
	<dl>
		<dt>是否是菜单：</dt>
		<dd>
			<p><input type="checkbox" id="is_menu" class="checkbox"></p>
		</dd>
	</dl>
	<dl>
		<dt>是否控制权限：</dt>
		<dd>
			<p>
				<input id="is_control_auth" type="checkbox" class="checkbox" checked="checked" />
			</p>
		</dd>
	</dl>
	<dl>
		<dt>是否开发者模式可见：</dt>
		<dd>
			<p><input id="is_dev" type="checkbox" class="checkbox" checked="checked" /></p>
		</dd>
	</dl>
	<dl>
		<dt>排序：</dt>
		<dd>
			<p><input id="sort" type="number" value="0" class="input-common harf"></p>
			<p class="error">请输入序号</p>
		</dd>
	</dl>
	<dl>
		<dt>图片：</dt>
		<dd>
			<div class="upload-btn-common">
				<div>
					<input class="input-file" name="file_upload" id="uploadImg" type="file" onchange="imgUpload(this);" title="上传">
					<input type="hidden" id="Logo" value="">
				</div>
				<input type="text" id="text_Logo" class="input-common" readonly="readonly" value="">
				<em>上传</em>
				<img id="preview_Logo" src="__STATIC__/blue/img/upload-common-select.png" data-src="" data-html="true" data-container="body" data-placement="top" data-trigger="manual" data-original-title="" title="">
			</div>
			<p class="error">请上传图片</p>
		</dd>
	</dl>
	<dl>
		<dt>描述：</dt>
		<dd>
			<p><textarea id="desc" class="textarea-common"></textarea></p>
			<p class="error">请输入模块描述</p>
		</dd>
	</dl>
	<dl>
		<dt></dt>
		<dd>
			<button class="btn-common btn-big" onclick="addModuleAjax()">保存</button>
			<button class="btn-common-cancle btn-big" onclick="javascript:history.back(-1);" >返回</button>
		</dd>
	</dl>
</div>
<script src="__STATIC__/js/ajax_file_upload.js" type="text/javascript"></script>
<script src="__STATIC__/js/file_upload.js" type="text/javascript"></script>
<script>
//图片上传
function imgUpload(event) {
	var fileid = $(event).attr("id");
	var id = $(event).next().attr("id");
	var data = { 'file_path' : "system_module" };
	uploadFile({
		url: __URL(ADMINMAIN + '/system/uploadimage'),
		fileId: fileid,
		data : data,
		callBack: function (res) {
			if(res.code){
				$("#" + id).val(res.data.path);
				$("#text_" + id).val(res.data.path);
				$("#preview_"+ id).attr("data-src",__IMG(res.data.path));
				showTip(res.message,"success");
			}else{
				showTip(res.message,"error");
			}
		}
	});
}

//模块输入信息验证
function verify(module_name, controller, method, url,  sort, desc){
	if(module_name == ''){
		$("#module_name").parent().next().show();
		return false;
	}else{
		$(".error").hide();
	}
	if(controller == ''){
		$("#controller").parent().next().show();
		return false;
	}else{
		$(".error").hide();
	}
	if(method == ''){
		$("#method").parent().next().show();
		return false;
	}else{
		$(".error").hide();
	}
	if(url == ''){
		$("#url").parent().next().show();
		return false;
	}else{
		$(".error").hide();
	}
	return true;
}

var flag = false;//防止重复提交
function addModuleAjax() {
	var pid = $("#pid").val();
	var module_name = $("#module_name").val();
	var controller = $("#controller").val();
	var method = $("#method").val();
	var url = $("#url").val();
	var is_menu = $("#is_menu").val();//是否是菜单
	var is_control_auth = 1;//是否控制权限，默认控制
	var is_dev = $("#is_dev").val();//是否仅开发者模式可见
	var sort = $("#sort").val();
	var desc = $("#desc").val();
	if($("#is_menu").prop("checked")){
		var is_menu = 1;
	}else{
		var is_menu = 0;
	}
	if(!$("#is_control_auth").prop("checked")){
		is_control_auth = 0;
		}
	if($("#is_dev").prop("checked")){
		var is_dev = 1;
	}else{
		var is_dev = 0;
	}
	var module_picture = $("#Logo").val();
	if(verify(module_name, controller, method, url, sort, desc)){
		if(flag){
			return;
		}
		flag = true;
		$.ajax({
			type : "post",
			url : "{:__URL('ADMIN_MAIN/system/addmodule')}",
			data : {
				'pid' : pid,
				'module_name' : module_name,
				'controller' : controller,
				'method' : method,
				'url' : url,
				'is_menu' : is_menu,
				'is_control_auth' : is_control_auth,
				'is_dev' : is_dev,
				'sort' : sort,
				'module_picture' : module_picture,
				'desc' : desc
			},
			success : function(data) {
				if (data["code"] > 0) {
					showTip(data["message"],'success');
					location.href = "{:__URL('ADMIN_MAIN/system/modulelist')}";
				}else{
					showTip(data["message"],'error');
					flag = false;
				}
			}
		});
	}
}
</script>
{/block}